package com.recruit.g1.user.service.impl;

import com.recruit.g1.user.mapper.VerificationMapper;
import com.recruit.g1.user.pojo.dto.VerificationParam;
import com.recruit.g1.user.pojo.entity.Verification;
import com.recruit.g1.user.pojo.vo.VerificationVO;
import com.recruit.g1.user.service.VerificationService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class VerificationServiceImpl implements VerificationService {

    @Resource
    private VerificationMapper verificationMapper;

    @Override
    public Verification submitVerification(VerificationParam param) {
        Verification verification = new Verification();
        BeanUtils.copyProperties(param, verification);
        verification.setStatus((byte) 0); // 默认未审核状态
        verification.setCreatedAt(new Date());
        verification.setUpdatedAt(new Date());
        verificationMapper.insert(verification);
        return verification;
    }


    @Override
    public VerificationVO getVerificationById(Long id) {
        Verification verification = verificationMapper.selectById(id);
        if(verification==null){
            verification = new Verification();
            verification.setStatus((byte)3);
            return convertToVO(verification);
        }
        return convertToVO(verification);

    }


    @Override
    public Verification getVerificationByIdNumber(String idNumber) {
        return verificationMapper.selectByIdNumber(idNumber);
    }

    @Override
    public List<VerificationVO> getAllVerifications() {
        List<Verification> verifications = verificationMapper.selectAll();
        return verifications.stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());
    }

    @Override
    public boolean approveVerification(Long id) {
        return verificationMapper.updateStatus(id, (byte) 1) > 0;
    }

    @Override
    public boolean rejectVerification(Long id) {
        return verificationMapper.updateStatus(id, (byte) 2) > 0;
    }

    @Override
    public boolean updateVerification(Long id, VerificationParam param) {
        Verification verification = new Verification();
        BeanUtils.copyProperties(param, verification);
        verification.setId(id);
        verification.setStatus(new Byte("0"));
        verification.setUpdatedAt(new Date());
        System.out.println("updateVerification--url"+param.getStatus());
        int rs=verificationMapper.updateById(verification);
        System.out.println(rs);
        return  rs> 0;
    }



    private VerificationVO convertToVO(Verification verification) {
        if (verification == null) {
            return null;
        }

        VerificationVO vo = new VerificationVO();
        BeanUtils.copyProperties(verification, vo);

        // 转换状态为文本
        switch (verification.getStatus()) {
            case 0:
                vo.setStatusText("0");//0 正在审核
                break;
            case 1:
                vo.setStatusText("1");//1审核通过
                break;
            case 2:
                vo.setStatusText("2");//2审核拒绝
                break;
            default:
                vo.setStatusText("3"); //3未实名
        }

        return vo;
    }
}